<?php require_once('../Connections/locale.php'); ?>
<?php
mysql_select_db($database_locale, $locale);
$query_emails = "SELECT * FROM email_predefinite";
$emails = mysql_query($query_emails, $locale) or die(mysql_error());
$row_emails = mysql_fetch_assoc($emails);
$totalRows_emails = mysql_num_rows($emails);

mysql_select_db($database_locale, $locale);
$query_ricerca = "SELECT * FROM abbonati_riviste, abbonati, riviste where abbonati.id_abbonato = abbonati_riviste.id_abbonato and abbonati_riviste.id_rivista = riviste.id_rivista and abbonati.email is not null ";

if (isset($_POST['id_rivista'])) {
  if ($_POST['id_rivista'] != "-1") $query_ricerca .= " and riviste.id_rivista=".$_POST['id_rivista'];
}

if (isset($_POST['id_tipologia'])) {
  if ($_POST['id_tipologia'] != "-1") $query_ricerca .= " and id_tipologia=".$_POST['id_tipologia'];
}

if (isset($_POST['id_categoria'])) {
  if ($_POST['id_categoria'] != "-1") $query_ricerca .= " and id_categoria=".$_POST['id_categoria'];
}

if (isset($_POST['id_stato_fattura'])) {
  if ($_POST['id_stato_fattura'] != "-1") $query_ricerca .= " and id_stato_fattura=".$_POST['id_stato_fattura'];
}

if (isset($_POST['pagamento'])) {
  if ($_POST['pagamento'] != "-1") {
  		if ($_POST['pagamento'] == "0") {
	  	 	$query_ricerca .= " and pagato_il is null ";
		} else {
			$query_ricerca .= " and pagato_il is not null ";
		}
	 }
}

if (isset($_POST['id_commissionaria'])) {
  if ($_POST['id_commissionaria'] != "-1") $query_ricerca .= " and id_commissionaria=".$_POST['id_commissionaria'];
}

if (isset($_POST['scadenza_da'])) {
  if ($_POST['scadenza_da'] != "") $query_ricerca .= " and data_scadenza >='".$_POST['scadenza_da']."'";
}


if (isset($_POST['scadenza_a'])) {
  if ($_POST['scadenza_a'] != "") $query_ricerca .= " and data_scadenza <='".$_POST['scadenza_a']."'";
}

$ricerca = mysql_query($query_ricerca, $locale) or die(mysql_error());
$row_ricerca = mysql_fetch_assoc($ricerca);
$totalRows_ricerca = mysql_num_rows($ricerca);
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
<link href="../style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<table border="0" id="datatable">
  <tr>
    <td><strong>Nominativo</strong></td>
    <td><strong>Rivista</strong></td>
    <td><strong>Scadenza</strong></td>
  </tr>
  <?php do { ?>
    <tr>
	   <td><?php echo $row_ricerca['nominativo']; ?></td>
   	   <td><?php echo $row_ricerca['descrizione']; ?></td>
      <td><?php echo $row_ricerca['data_scadenza']; ?></td>
    </tr>
    <?php } while ($row_ricerca = mysql_fetch_assoc($ricerca)); ?>
</table>
</fieldset>

<form action="invio_emails.php" method="post" name="form1" target="_self" id="form1">
  <p>Seleziona Email:
    <select name="id_email" id="id_email">
      <?php
do {  
?>
      <option value="<?php echo $row_emails['id_email']?>"><?php echo $row_emails['subject']?></option>
      <?php
} while ($row_emails = mysql_fetch_assoc($emails));
  $rows = mysql_num_rows($emails);
  if($rows > 0) {
      mysql_data_seek($emails, 0);
	  $row_emails = mysql_fetch_assoc($emails);
  }
?>
      </select>
    <input name="id_rivista" type="hidden" value="<?php echo $_GET['id_rivista']; ?>" />
    <input type="hidden" name="hiddenField" />
    <input name="query" type="hidden" id="query" value="<?php echo $query_ricerca; ?>" />
  </p>
  <p>
    <label>
    <input type="submit" name="Submit" value="Invia" />
    </label>
</p>
</form>

</body>
</html>
<?php
mysql_free_result($emails);

mysql_free_result($ricerca);
?>
